<template>
  <div class="login">
    <h1>我是登陆页面</h1>
    <el-form ref="form" :model="form" :rules="rules" class="container">
      <el-form-item prop="username">
        <el-input v-model="form.username" placeholder="请输入用户名" prefix-icon="el-icon-search"></el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="form.password" placeholder="请输入密码" prefix-icon="el-icon-search"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" class="login-btn" @click="submitForm('form')">登陆提交</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { checkLogin } from "@/api/"; // 默认会去找index.js 文件
// import {checkLogin} from  '@/api/index.js'  //
export default {
  name: "",
  components: {},
  data() {
    return {
      form: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 3, max: 15, message: "长度在 3 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.form);
          checkLogin(this.form).then(res => {
            // 1登陆成功之后需要把token 在本地缓存里存
            // 2 成功之后是应该跳转到home页面

            if (res.meta.status == 200) {
              // 1登陆成功之后需要把token 在本地缓存里存
              localStorage.setItem("myToken", res.data.token);
              // 2 成功之后是应该跳转到home页面
              //name :是定义的路由那里的name

              //           {
              //   path: '/home',
              //   name: 'home',
              //   component: Home
              // }
              this.$router.push({ name: "home" });
            } else {
              this.$message({
                showClose: true,
                message: res.meta.msg,
                type: "error"
              });
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    }
  }
};
</script>



<style scoped    lang="scss">
//  采用 scss 写css 样式
// 1  先装  node-sass
// 2 sass-node
// // cnpm install node-sass --save-dev
// // cnpm install sass-loader --save-dev

.login {
  position: fixed;
  width: 100%;
  height: 100%;
  background-color: #2f4050;
  h1 {
    text-align: center;
    color: white;
  }

  .container {
    position: absolute;
    left: 0;
    right: 0;
    width: 400px;
    padding: 15px 40px 15px 40px;
    margin: 70px auto;
    background: white;
    .avatar {
      position: relative;
      left: 50%;
      width: 120px;
      height: 120px;
      margin-left: -60px;
      margin-top: -60px;
      box-sizing: border-box;
      border-radius: 50%;
      border: 10px solid #fff;
      box-shadow: 0 1px 5px #ccc;
      overflow: hidden;
    }
    .login-btn {
      width: 100%;
    }
  }
}
</style>
